﻿using Core.Dtos;
using Core.IRepository;
using Core.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Core.Enums;

namespace Services
{
    public class ArticleService : IArticleService
    {
        IUnitOfWork UnitOfWork;
        IArticleRepository ArticleRepository;
        IColumnRepository ColumnRepository;

        public ArticleService(IUnitOfWork UnitOfWork, IArticleRepository ArticleRepository, IColumnRepository ColumnRepository)
        {
            this.ArticleRepository = ArticleRepository;
            this.ColumnRepository = ColumnRepository;
            this.UnitOfWork = UnitOfWork;
        }

        public void CreateDefaultLists(string userID)
        {
            AddList(userID, "ToDos", ArticleType.List, new string[] { "Message" });
            AddList(userID, "Shopping", ArticleType.List, new string[] { "Message" });
            AddList(userID, "Links", ArticleType.Tabular, new string[] { "Description", "Link" });
        }      

        public void AddList(string userID , string name, ArticleType type, string[] columnNames)
        {
            ArticleDto articleDto = new ArticleDto() { Name = name };
            articleDto.UserID = userID;
            articleDto.TypeID = (int)type;

            Core.Entities.Article article = articleDto.ToEntity();
            ArticleRepository.Add(article);
            UnitOfWork.Commit();

            Core.Entities.Column column;
            for (int i = 0; i < columnNames.Length; i++)
            {
                column = new Core.Entities.Column() { ArticleID = article.ID, Name = columnNames[i] };
                ColumnRepository.Add(column);
            }            
            UnitOfWork.Commit();            
        }
    }
}
